<?php
/**
 * Created by PhpStorm.
 * User: xh
 * Date: 2020/4/22
 * Time: 5:25 PM
 */

namespace app\model\weChat;


use think\Model;

class WeChatLive extends Model
{
    /** 列表
     * 1.plat-live-Major-list
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function platList($page,$limit){
//        $cond = [['live_status','in',[101,102]],['is_delete','=',0],['is_show','=',0]];
        $cond = [['is_delete','=',0],['is_show','=',0]];
        $count = $this
            ->where($cond)
            ->count();
        if(!$count)
        {return ['list'=>[],'page'=>0,'pages'=>0,'count'=>0,'limit'=>$limit];}
        $list = $this
            ->where($cond)
            ->order('sort desc')
            ->limit($this->paging($page,$limit),$limit)
            ->select();
        $newList = [];
        foreach ($list as $k => $v){
            $newList[] = [
                'id' => $v['id'],
                'roomId' => $v['room_id'],
                'liveStatus' => $v['live_status'],
                'coverImg' => $v['share_img'],
                'shareImg' => $v['cover_img'],
                'startTime' => date('Y-m-d H:i:s',$v['start_time']),
                'endTime' => date('Y-m-d H:i:s',$v['end_time']),
                'number' => $v['number'],
                'headline' => empty($v['headline'])?$v['room_name']:$v['headline'],
                'subhead' => $v['subhead'],
                'liveName' => empty($v['live_name'])?$v['anchor_name']:$v['live_name'],
                'liveAvatar' => empty($v['live_avatar'])?'':ImageAddDomain($v['live_avatar']),
            ];
        }
        return ['list'=>$newList,'page'=>(int)$page,'pages'=>$this->pages($count,$limit),'count'=>$count,'limit'=>$limit];
    }

    /** 更新(微信的数据需要全部更新一次)
     * 1.back-weChat-Live-renewal
     * @param string $identity
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function renewal($identity='A'){
        $list = [];
        $res = WeChat('Serve')->liveList($list,0,100,'M');
        if(empty($res)){return false;}
        $WeChatLiveLog = new WeChatLiveLog();
        $WeChatLiveLog->add($list,$identity);
        $haveList = $this->where(['identity'=>$identity])->select();
        $rawArr = [];
        $rawData = [];
        foreach ($haveList as $key => $val){
            $rawArr[] = (int)$val['room_id'];
            $rawData[(int)$val['room_id']] = [
                'id' => $val['id']
            ];
        }
        $arr = [];
        $liveData = [];
        foreach ($list as $key => $v){
            $arr[] = (int)$v['roomid'];
            $liveData[(int)$v['roomid']] = [
                'identity' => $identity,
                'room_name' => $v['name'],
                'room_id' => $v['roomid'],
                'cover_img' => $v['cover_img'],
                'start_time' => $v['start_time'],
                'end_time' => $v['end_time'],
                'anchor_name' => $v['anchor_name'],
                'live_status' => $v['live_status'],
                'share_img' => $v['share_img'],
                'goods_info' => empty($v['goods'])?'':Base64En(JsonEn($v['goods'])),
            ];
        }
        //差集
        $delArr = array_diff($rawArr,$arr);
        if(!empty($delArr)){
            $this->where('identity','=',$identity)
                ->where('room_id','in',$delArr)
                ->update(['is_delete'=>1,'delete_time'=>DateTime(time())]);
        }
        //交集
        $editArr = array_intersect($rawArr,$arr);
        if( !empty($editArr) ){
            $editData = [];
            foreach ($editArr as $k => $v){
                $info = $liveData[$v];
                $info['id'] = $rawData[$v]['id'];
                $editData[] = $info;
            }
            $this->saveAll($editData);
        }
        $addArr = array_diff($arr,$rawArr);
        if( !empty($addArr) ){
            $addData = [];
            foreach ($addArr as $k => $v){
                $addData[] = $liveData[$v];
            }
            $this->insertAll($addData);
        }
        return true;
    }

    /** 列表
     * 1.back-weChat-Live-list
     * @param $roomName
     * @param $anchorName
     * @param $headline
     * @param $isShow
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list($roomName,$anchorName,$headline,$isShow,$page,$limit){
        $cond = [['is_delete','=',0]];
        if(!empty($groupingId))
        {$cond[] = ['grouping_id','=',$groupingId];}
        if(in_array($isShow,['0','1']))
        {$cond[] = ['is_show','=',$isShow];}
        if(!empty($roomName))
        {$cond[] = ['room_name','like','%'.$roomName.'%'];}
        if(!empty($headline))
        {$cond[] = ['headline','like','%'.$headline.'%'];}
        if(!empty($anchorName))
        {$cond[] = ['anchor_name|live_name','like','%'.$anchorName.'%'];}
        $count = $this
            ->where($cond)
            ->count();
        if(!$count)
        {return ['list'=>[],'page'=>0,'pages'=>0,'count'=>0,'limit'=>$limit];}
        $list = $this
            ->where($cond)
            ->order('sort desc')
            ->limit($this->paging($page,$limit),$limit)
            ->select();
        $newList = [];
        foreach ($list as $k => $v){
            $newList[] = [
                'id' => $v['id'],
                'roomName' => $v['room_name'],
                'roomId' => $v['room_id'],
                'coverImg' => $v['cover_img'],
                'startTime' => date('Y-m-d H:i:s',$v['start_time']),
                'endTime' => date('Y-m-d H:i:s',$v['end_time']),
                'anchorName' => $v['anchor_name'],
                'liveStatus' => $v['live_status'],
                'shareImg' => $v['share_img'],
                'isShow' => $v['is_show'],
                'sort' => $v['sort'],
                'number' => $v['number'],
                'headline' => $v['headline'],
                'subhead' => $v['subhead'],
                'liveName' => $v['live_name'],
                'liveAvatar' => empty($v['live_avatar'])?'':ImageAddDomain($v['live_avatar']),
            ];
        }
        return ['list'=>$newList,'page'=>(int)$page,'pages'=>$this->pages($count,$limit),'count'=>$count,'limit'=>$limit];
    }

    /** 编辑
     * 1.back-weChat-Live-hold
     * @param $id
     * @param $headline
     * @param $subhead
     * @param $liveName
     * @param $liveAvatar
     * @param $number
     * @param $sort
     * @param $isShow
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit($id,$headline,$subhead,$liveName,$liveAvatar,$number,$sort,$isShow){
        $info = $this->where([['id','=',$id],['is_delete','=',0]])->find();
        if(empty($info)){return 'absent';}
        $info->headline = $headline;
        $info->subhead = $subhead;
        $info->live_name = $liveName;
        $info->live_avatar = ImageToContain($liveAvatar);
        $info->number = $number;
        $info->sort = $sort;
        $info->is_show = $isShow;
        return $info->save();
    }

    /** 显示
     * 1.back-weChat-Live-show
     * @param $id
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function show($id){
        $info = $this->where([['id','=',$id],['is_delete','=',0]])->find();
        if(empty($info)){return 'absent';}
        $info->is_show = empty($info->is_show)?1:0;
        return $info->save();
    }
}